package com.atguigu.spzx.manager.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.spzx.common.exception.XiaoHeiException;
import com.atguigu.spzx.manager.listener.ExcelListener;
import com.atguigu.spzx.manager.mapper.CategoryMapper;
import com.atguigu.spzx.manager.service.CategoryService;
import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.product.CategoryExcelVo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: CategoryServiceImpl
 * Package:
 * Description:
 *
 * @Author:xiaohei
 * @Create 2024/5/19 17:34
 * Version 1.0
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> findCategoryList(Long id) {
        //1 根据id条件值进行查询
        //select * from category Where parent_id = id
        List<Category> categoryList = categoryMapper.selectCategoryByParentId(id);

        //2 遍历返回的list集合
        // 判断每个分类是否有下一层分类  如果有设置hasChildren = true
        if(!CollectionUtils.isEmpty(categoryList)){
            categoryList.forEach(category -> {
                //判断每个分类是否有下一层分类
                int count = categoryMapper.selectCountByParentId(category.getId());
                if(count > 0){
                    category.setHasChildren(true);
                }else{
                    category.setHasChildren(true);
                }
            });
        }

        return categoryList;
    }

    @Override
    public void exportData(HttpServletResponse response) {
        try{
            //1 设置响应头信息和其他信息
            // 设置响应结果类型
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");

            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("分类数据", "UTF-8");

            //设置响应头  Content-disposition 表示文件以下载方式打开
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            //2  调用mapper方法查询所有分类，返回list集合
            List<Category> categoryList = categoryMapper.findAll();

            //List<Category>   List<CategoryExcelVo>
            List<CategoryExcelVo> categoryExcelVoList = new ArrayList<CategoryExcelVo>();
            for (Category category : categoryList){
                CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
                //属性拷贝
                BeanUtils.copyProperties(category,categoryExcelVo);
                categoryExcelVoList.add(categoryExcelVo);
            }

            //3 调用EasyExcel的write方法完成导出
            EasyExcel.write(response.getOutputStream(), CategoryExcelVo.class)
                    .sheet("分类数据").doWrite(categoryExcelVoList);
        }catch(Exception e){
            e.printStackTrace();
            throw new XiaoHeiException(ResultCodeEnum.DATA_ERROR);
        }
    }

    @Override
    public void importData(MultipartFile file) {
        ExcelListener<CategoryExcelVo> listener = new ExcelListener(categoryMapper);
        try {
            EasyExcel.read(file.getInputStream(), CategoryExcelVo.class,listener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new XiaoHeiException(ResultCodeEnum.DATA_ERROR);
        }
    }
}
